<?php
//last fix: 7 april 2008
//little anchor bug with visual integration

// Inspired from the work of Simon Liddicott 2004
// and Stephen Trembath August 2004
// Re-Written by Guessous Mehdi 2006 http://www.mehdiplugins.com/misc
// Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html

if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

global $THEME_DIR;
global $BRIDGE;
global $CONFIG;
global $joomRel;
global $joomlaOutput;


$joomRel=$BRIDGE['relative_path_to_config_file'];
$joomUrl=$BRIDGE['full_forum_url'];
$scriptUrl=$CONFIG['site_url']; //correspond to ecards_more_pic_target in config table, silly
//warning! $scriptUrl end up with a slash !
//let's remove it to have a variable similar to $joomUrl
$scriptUrl=rtrim($scriptUrl,"/");

$template_file = $THEME_DIR . TEMPLATE_FILE; //reminder, already tested that file exists in functions.inc.php
$scriptOutput= fread($fileres = fopen($template_file, 'r'), filesize($template_file));
fclose($fileres);
if(preg_match('#<html[^>]*>.*?<head[^>]*>(.*?)</head>.*?<body[^>]*>(.*)</body>.*?</html>#si',$scriptOutput,$matches)){
		   $scriptOutputHead=$matches[1];
		   $scriptOutputBody=$matches[2];
			 unset($matches[1]);
			 unset($matches[2]);
			 unset($scriptOutput);
}
else die("Please ensure that the file <i>template.html</i> of your current Coppermine's theme has a valid Xhtml syntax. <br/>
Especially check the \"html\", \"head\",  \"body\" tags.");

$finalOutput= $joomlaOutput; //this is the joomla output retrieved by joomla.inc.php

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//------ all call back functions -----------------------------------------------

  //--- callback func used to fix links in javascripts
 function scriptFixCallback($matches){
 	  global $joomRel;

 	  //fix link especially for the template changer
	  $value=preg_replace('#\.src\s*=\s*([\'"])([^/:\\1][^:\\1]*)\\1#i','.src=\1'. $joomRel . '\2\1',$matches[2]);
	  //--- fix for modules that use ajax, especially Who's Online XT
	  $value=preg_replace('#=\s*([\'"])(modules/mod_[^:\\1\.]*\.php)\\1#i','=\1'. $joomRel . '\2\1',$value);

   	$value='<script'. $matches[1] .'>'. $value . '</script>';
	  return $value;
 }

 //----- callback func used to fix embedded styles
 function styleFixCallback($matches){
 	 global $joomRel;
 	 $value=preg_replace('#url\s*\(([^/:\)][^:\)]*)\)#i','url('. $joomRel . '\1)', $matches[2]);
 	 $value='<style'. $matches[1] .'>'. $value . '</style>';
	 return $value;
 }

  //-------------------------------------------------------------------------------
  //-------------------------------------------------------------------------------
  $joomConf=& JFactory::getConfig();    
  
//replace base tag for sef , don't forget the ending slash
  if($joomConf->getValue('config.sef')) {
    $finalOutput=preg_replace('#<base\s*href\s*=[^>]*>#i','<base href="' . $scriptUrl . '/" />',$finalOutput);
  }

  //Fix the links in the included page to refer to the Joomla root directory
  //all below are attributes, so must begin by a space, removed: link, not a valid attribute
  //exclude links , that are "pure" anchors
    
	$finalOutput=preg_replace('#(<[^>]*\s(?:href|src|action|background))\s*=\s*"([^/:"\#][^:"]*)"#i','\1="'. $joomRel .'\2"',$finalOutput);

	//Fix param tag for flash only
	$finalOutput=preg_replace('#<param\s+name="movie"\s+value="([^/:"][^:"]*\.swf[\?"])#i','<param name="movie" value="'. $joomRel .'\1',$finalOutput);

	 //----- Fix login module
	 //----- don't forget the ending slash for $scriptUrl
	 $returnVal= base64_encode($scriptUrl . "/");
	 $finalOutput=preg_replace('#<input\s+[^>]*name="return"\s+value="[A-Za-z0-9+/=]+"[^>]*>#i','<input name="return" value="'  . $returnVal . '" type="hidden" />',$finalOutput);

	//fix links in javascripts, especially the template changer
	$finalOutput=preg_replace_callback('#<script([^>]*)>(.*?)</script>#si',"scriptFixCallback", $finalOutput);

	//fix embedded styles
  $finalOutput=preg_replace_callback('#<style([^>]*)>(.*?)</style>#si',"styleFixCallback", $finalOutput);

  //-------------------------------------------------------------------------------
	//-------------------------------------------------------------------------------
  //remove joomla's title will be overridden by script's one
  $finalOutput=preg_replace('#<title([^>]*)>.*?</title>#si','',$finalOutput);

 	//------------- put any coppermine addition at the end
 	//if an alternative css exist use it instead of the default one
 	if(file_exists($THEME_DIR."style2.css"))
 		$scriptOutputHead=preg_replace('#<link([^>]*)href="([^>\"]*)style.css"([^>]*)>#i','<link\1href="\2style2.css"\3>',$scriptOutputHead);

 	 $scriptOutputHead=	"\n<!-- +++++++++++++++++++ Script head +++++++++++++++++++ -->\n" . $scriptOutputHead;

 	//Merge coppermine head to joomla's head
	$finalOutput=preg_replace('#</head>#i',$scriptOutputHead . "\n</head>",$finalOutput,1);    //replace first occurrence only
	                                                                            //if there's two, something is wrong

  //insert coppermine template
 	$finalOutput=str_replace('{COPPERMINE_TEMPLATE_HTML}',$scriptOutputBody,$finalOutput);

  $template=$finalOutput; unset($finalOutput);
  //echo $template; //do not output $template !!!!!!!!!!!!!!
?>